package  compoments
{
	import flash.display.DisplayObject;
	
	import mx.controls.List;
	import mx.controls.listClasses.ListData;
	import mx.controls.listClasses.ListItemRenderer;
	import mx.core.IFlexDisplayObject;
	import mx.core.IUITextField;
	import mx.core.UITextField;

	public class ContactListItemRenderer extends ListItemRenderer
	{
		
		protected var statusIcon : IFlexDisplayObject;
    
	    protected var humorLabel : IUITextField ;
	    
	    protected var statusLabel : IUITextField ;
	    
	    private var listOwner:List;
	    
	   /*  private var nameTextFormat :TextFormat ;
	    private var statusTextFormat : TextFormat ;
	    private var humorTextFormat :TextFormat ; */
	   
		public function ContactListItemRenderer()
		{
			super();
			/* statusTextFormat = new  TextFormat();
			statusTextFormat.color = 0xffff00 ;
			statusTextFormat.size = 12 ;
			
			humorTextFormat =new TextFormat() ;
			humorTextFormat.size = 14 ;
			humorTextFormat.color = 0xff0000 ; */
		}
		
		 override protected function createChildren():void
		{
	        super.createChildren();
			createHumorLabel(-1);
            createStatusLabel(-1);
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			 if (hasFontContextChanged() && humorLabel != null)
	        {
	      		var index:int = getChildIndex(DisplayObject(humorLabel));
	            removeHumorLabel();
	 			createHumorLabel(index); 
	        }

             if (hasFontContextChanged() && statusLabel != null)
	        {
	      		var index2:int = getChildIndex(DisplayObject(statusLabel));
	            removeStatusLabel();
	 			createStatusLabel(index2); 
	        }
	
			if (statusIcon)
			{
				removeChild(DisplayObject(statusIcon));
				statusIcon = null; 
			}
		//	400 在线
//600 忙碌
//100 离开
//0 隐身或离线	
			if(data!= null){
				listOwner = List(listData.owner);
				
				var statusIconString:String ="telephoneOffline";
				var status:String ="短信在线";
				
				if(data.devicetype=="PC"){
					
					statusIconString = "telephoneOnline";
					if(data.value == "400"){				   		
				   		status = "在线";
					}else if(data.value == "600")
					{
				   		status = "忙碌";
					}else if(data.value == "100")
					{
				   		status = "离开";
					}else if(data.value == "0")
					{
				   		status = "离线";
					}
					else
					{
				   		statusIconString="telephoneOffline";
						status="短信在线";
					}
					
				}else{
					statusIconString="telephoneOffline";
					status="短信在线";
				}
				
			
				
				   
				var statusIconClass:Class = Class(systemManager.getDefinitionByName(statusIconString));
				
			    if(statusIconClass)
			        statusIcon = new statusIconClass();
			    else
			        statusIcon = new document[statusIconString];
			    
			    addChild(DisplayObject(statusIcon));
			    
			    humorLabel.text = data.impresa;
			    humorLabel.multiline = listOwner.variableRowHeight;
		        humorLabel.wordWrap = listOwner.wordWrap;
		        
		      //  humorLabel.setTextFormat(this.humorTextFormat);
                
                statusLabel.text = status ;
		        statusLabel.multiline = listOwner.variableRowHeight;
		        statusLabel.wordWrap = listOwner.wordWrap;
		       // statusLabel.setTextFormat(this.statusTextFormat);
			
				
				
				
			 }
			else {
				humorLabel.text = null;
				statusLabel.text = null;
				label.text = null; 
				
			}
		    invalidateDisplayList(); 
		} 
		
		
		
		override protected function updateDisplayList(unscaledWidth:Number,
												  unscaledHeight:Number):void
		{
			
			super.updateDisplayList(unscaledWidth, unscaledHeight);
	
			var startx:Number = data ? ListData(listData).columnIndex : 0;
						
			if (icon)
			{
				icon.x = startx;				
				startx = icon.x + icon.measuredWidth;
				icon.setActualSize(icon.measuredWidth, icon.measuredHeight);
			}
			
			if(statusIcon)
			{
			    statusIcon.x = startx ;
				startx = statusIcon.x + statusIcon.measuredWidth;
				statusIcon.setActualSize(statusIcon.measuredWidth,statusIcon.measuredHeight);
			}
			
			if(humorLabel)
			{
				humorLabel.x = startx ;
				humorLabel.setActualSize(unscaledWidth - startx, measuredHeight);
			}
 
            if(statusLabel)
			{
				statusLabel.x = startx+label.textWidth + 10;
				//statusLabel.width =50;
				statusLabel.setActualSize(unscaledWidth - startx, measuredHeight);
			}
			
			label.x = startx;
			label.setActualSize(unscaledWidth - startx, measuredHeight);
	        
			// top
			label.y = -5;
			statusLabel.y = -5 ;
					
			// bottom
			humorLabel.y = 8; // 2 for gutter
			
			
		   }
		
	
		
		
		private function removeHumorLabel():void{
			if (humorLabel != null)
	        {
	        	removeChild(DisplayObject(humorLabel));
	        	humorLabel = null;
	        }
		}
		
		private function createHumorLabel(childIndex:int):void{
			if (!humorLabel)
	        {
				humorLabel = IUITextField(createInFontContext(UITextField));
				//humorLabel = IUITextField(new UITextField() );
				//humorLabel.setTextFormat(humorTextFormat);
				
				humorLabel.styleName = this;
				
	            if (childIndex == -1)
	                addChild(DisplayObject(humorLabel));
	            else 
	                addChildAt(DisplayObject(humorLabel), childIndex);
	        }
		}
        
        private function removeStatusLabel():void{
			if (statusLabel != null)
	        {
	        	removeChild(DisplayObject(statusLabel));
	        	statusLabel = null;
	        }
		}
		
		private function createStatusLabel(childIndex:int):void{
			if (!statusLabel)
	        {
				statusLabel = IUITextField(createInFontContext(UITextField));
				statusLabel.styleName = this;
				//statusLabel =  IUITextField(new UITextField());
				//statusLabel.setTextFormat(this.statusTextFormat);
				
	            if (childIndex == -1)
	                addChild(DisplayObject(statusLabel));
	            else 
	                addChildAt(DisplayObject(statusLabel), childIndex);
	        }
		}
		
		
		
	}
}